package com.uw.gateway.config;

import com.uw.gateway.filter.UwGatewayFilter;
import com.uw.gateway.filter.UwGatewayPostFilter;
import com.uw.gateway.handler.web.UwGatewayWebHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;

import java.util.Properties;

@SpringBootConfiguration
public class UwGatewayConfig {

    @Bean
    public UwGatewayWebHandler uwGatewayWebHandler() {
        return new UwGatewayWebHandler();
    }

    @Bean
    public UwGatewayFilter uwGatewayFilter() {
        return new UwGatewayFilter();
    }

    @Bean
    public UwGatewayPostFilter uwGatewayPostFilter() {
        return new UwGatewayPostFilter();
    }


    /**
     * 通过  SimpleUrlHandlerMapping 注入自定义的 web handler
     */
    @Bean
    public ApplicationRunner runner(@Autowired ApplicationContext applicationContext) {
        return args -> {
            SimpleUrlHandlerMapping simpleUrlHandlerMapping = applicationContext.getBean(SimpleUrlHandlerMapping.class);
            Properties mappings = new Properties();
            mappings.setProperty("/gw/**", "uwGatewayWebHandler");
            simpleUrlHandlerMapping.setMappings(mappings);
            simpleUrlHandlerMapping.initApplicationContext();
        };
    }

}
